//
//  NRWTemplateAlertView.m
//  New_Runway
//
//  Created by 邢毅 on 2018/5/30.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWTemplateAlertView.h"

@interface NRWTemplateAlertView()

@property (strong, nonatomic) IBOutlet UITextView *tempTextView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeight;
@property (strong, nonatomic) IBOutlet UIView *contentView;

@end

CGFloat normalHeight = 288;

@implementation NRWTemplateAlertView
@synthesize contentText = _contentText;

- (instancetype)init
{
    self = [[NSBundle mainBundle] loadNibNamed:@"NRWTemplateAlertView" owner:self options:nil].firstObject;
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];
        
        self.contentViewHeight.constant = self.contentViewHeight.constant + getSafeBottomMargin();
    }
    return self;
}

- (void)keyboardWillShow:(NSNotification *)notification {
    
    CGFloat keyboardH = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.contentView.frame = CGRectMake(0, Screen_height - normalHeight - keyboardH, Screen_width, normalHeight);
    }];
}

- (void)keyboardWillHidden:(NSNotification *)notification {
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.contentView.frame = CGRectMake(0, Screen_height - normalHeight - getSafeBottomMargin(), Screen_width, normalHeight + getSafeBottomMargin());
    }];
}

- (void)setContentText:(NSString *)contentText {
    
    _contentText = contentText;
    
    self.tempTextView.text = contentText;
}

- (NSString *)contentText {
    
    return self.tempTextView.text;
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
    
    [self endEditing:YES];
}

- (IBAction)closeBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(templateAlertView:closeBtnDidClick:)]) {
        
        [self.delegate templateAlertView:self closeBtnDidClick:sender];
    }
}

- (IBAction)notCopyBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(templateAlertView:notCopyDidClick:)]) {
        
        [self.delegate templateAlertView:self notCopyDidClick:sender];
    }
}

- (IBAction)copyBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(templateAlertView:copyDidClick:)]) {
        
        [self.delegate templateAlertView:self copyDidClick:sender];
    }
}

- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    self.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
    }];
}

- (void)dismiss {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

@end
